home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10161 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: nntp.onyx.net!claymoor
  2. From: Adam.Morris@octacon.co.uk (Adam Morris)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Simple question about constructors
  5. Date: Wed, 06 Mar 96 17:59:43 GMT
  6. Organization: Octacon Ltd
  7. Message-ID: <4hk29c$n0p@mulgave.octacon.co.uk>
  8. References: <4ha7d2$quc@cloner4.netcom.com>
  9. NNTP-Posting-Host: claymoor.onyx.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4ha7d2$quc@cloner4.netcom.com>, stefmit@ix.netcom.com wrote:
  13. >newer source, I came across a syntax in the form:
  14. >class_name :: class_name (arguments) : data_member1 (argument1), data_member2 
  15. >(argument2) {}
  16. >The portion I don't know the meaning of (though I assume is kind of 
  17. >intialization list, that would've appeared otherwise within {}, as far as I 
  18. >learned from my book), is the portion after ":".
  19. >I would kindly request some elaboration on this, especially concerning a 
  20. >statement in the source where I found this, similar to: "this is the only way 
  21. >the initialization can be done in some cases (!!!)". Does this mean I am not 
  22. >always able to define a constructor the way I've learned so far (in its {} 
  23. >body)?
  24.  
  25. Yep it's an initialisation list...  It's always best to initalise everything 
  26. possible like that than in the body of the constructor.  as far as MUST be 
  27. done... then const and reference members can only be initialised and never 
  28. assigned.  it's also preferable for efficiency reasons.  initialisation of a 
  29. string calls a string constructor once, assignment calls two constructors... 
  30. one to construct the string, and one to assign to it...
  31.  
  32. For a better discussion of why hunt down Effective C++ by Scott Meyers and 
  33. read Item 12...
  34.  
  35. I hope this has explained (at least roughly) what you wanted to know.
  36.  
  37. Adam.
  38.